home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2b.lha / p4-1.2b / servers / iserv_p4.c < prev    next >
C/C++ Source or Header  |  1992-10-19  |  3KB  |  155 lines

  1. #include "p4.h"
  2. #include "p4_sys.h"
  3.  
  4. extern int errno;
  5. extern char *sys_errlist[];
  6.  
  7. VOID reaper();
  8.  
  9. main(argc,argv)
  10. int argc;
  11. char **argv;
  12. {
  13.     int listen_fd, connection_fd;
  14.     int port;
  15.     int done;
  16.     struct sockaddr_in from;
  17.     int fromlen;
  18.     
  19.     p4_initenv(&argc,argv);
  20.     sprintf(whoami, "server"); /* alter p4 environment */
  21.  
  22.     net_setup_listener(5, UNRESERVED_PORT, &listen_fd);
  23.  
  24. #ifdef P4BSD
  25.     signal(SIGCHLD, reaper);
  26. #endif
  27. #ifdef P4SYSV
  28.     signal(SIGCLD, reaper);
  29. #endif
  30.  
  31.     done = FALSE;
  32.     while (!done)
  33.     {
  34.     fromlen = sizeof(from);
  35.     connection_fd = accept(listen_fd, &from, &fromlen);
  36.     if (connection_fd == -1)
  37.     {
  38.         if (errno == EINTR)
  39.         continue;
  40.         else
  41.         {
  42.         perror("server accept");
  43.         exit(1);
  44.         }
  45.     }
  46.     p4_dprintfl(20,"accepted on %d\n", connection_fd);
  47.     done = process_connection(connection_fd);
  48.     shutdown(connection_fd, 2);
  49.     close(connection_fd);
  50.     }
  51.  
  52.     shutdown(listen_fd, 2);
  53.     close(listen_fd);
  54. }
  55.  
  56. process_connection(fd)
  57. int fd;
  58. {
  59. struct net_message_t msg;
  60. int type;
  61. char *pgm;
  62. char *host;
  63. char *am_slave;
  64. int port;
  65.  
  66.     if (net_recv(fd, &msg, sizeof(msg)) == NET_RECV_EOF)
  67.     return(FALSE);
  68.  
  69.     type = p4_n_to_i(msg.type);
  70.  
  71.     switch (type)
  72.     {
  73.     case NET_EXEC:
  74.         pgm = msg.pgm;
  75.         host = msg.host;
  76.         am_slave = msg.am_slave;
  77.         port = p4_n_to_i(msg.port);
  78.         p4_dprintfl(20,"server got exec msg: pgm=%s host=%s port=%d am_slave=%s\n",
  79.            pgm, host, port, am_slave);
  80.         exec_pgm(host, pgm, port, am_slave);
  81.         msg.type = p4_i_to_n(NET_RESPONSE);
  82.         msg.success = p4_i_to_n(1);
  83.         net_send(fd, &msg, sizeof(msg), FALSE);
  84.         return(FALSE);
  85.  
  86.     case NET_DONE:
  87.         p4_dprintfl(20,"server got done message\n");
  88.         return(TRUE);
  89.  
  90.     default:
  91.         p4_dprintfl(20,"server got unknown message type %d\n", type);
  92.         return(FALSE);
  93.     }
  94. }
  95.     
  96. VOID exec_pgm(host, pgm, port, am_slave)
  97. char *host, *pgm, *am_slave;
  98. int port;
  99. {
  100. int pid;
  101. int rc;
  102. char shortpgm[100];
  103. char sport[10];
  104. char *s;
  105.  
  106.     if ((s = rindex(pgm, '/')) != NULL)
  107.     strcpy(shortpgm, s + 1);
  108.     else
  109.     strcpy(shortpgm, pgm);
  110.  
  111.     sprintf(sport, "%d", port);
  112.  
  113.     p4_dprintfl(20,"exec_pgm: pgm=%s short=%s sport=%s\n",
  114.             pgm, shortpgm, sport);
  115.  
  116.     fflush(stdout);
  117.     pid = fork();  /* Not fork_p4 here as don't want interrupts on error */
  118.     if (pid < 0)
  119.     p4_error("exec_pgm fork",pid);
  120.     if (pid == 0)
  121.     {
  122.     /* The child */
  123.  
  124.     /****
  125.       RB This line is typically used, but some versions
  126.       of exec seem to have a bug requiring the full pgm
  127.       name to be in both arg positions.
  128.  
  129.     rc = execl(pgm, shortpgm, host, sport, am_slave, NULL);
  130.     ****/
  131.  
  132.     rc = execl(pgm, pgm, host, sport, am_slave, NULL);
  133.  
  134.     if (rc < 0)
  135.         p4_error("exec_pgm execl",rc);
  136.     exit(1);
  137.     }
  138. }
  139.  
  140. VOID reaper()
  141. {
  142. int status;
  143.  
  144. int pid;
  145.     
  146.     p4_dprintfl(20,"server: entering reaper\n");
  147.     pid = wait(&status);
  148.     p4_dprintfl(20,"server: pid %d died with status %d\n", pid, status);
  149. }
  150.  
  151. int slave() /* dummy proc */
  152. {
  153.     return(0);
  154. }
  155.